<?php

namespace app\command;

use app\admin\model\AssembleList;
use app\admin\model\AssembleUser;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Good extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('good');
        // 设置参数
        
    }

    protected function execute(Input $input, Output $output)
    {
    	// 指令输出
        $spike = $this->spikeTime();
        $confirm = $this->spikeUser();
        $output->writeln($spike."\n".$confirm);
    }

    protected function spikeTime(){
        $times = AssembleList::select()->toArray();
        if ($times){
            foreach ($times as $key=>$value){
                $start_time = strtotime($value['start_time']);
                $end_time = strtotime($value['end_time']);
                //当前时间大于开始时间小于结束时间为活动中
                if (time() >= $start_time && time() < $end_time){
                    AssembleList::update(['status'=>1],['id'=>$value['id']]);
                }else {
                    //当前时间大于结束时间为已结束
                    if (time() > $end_time){
                        AssembleList::update(['status'=>0],['id'=>$value['id']]);
                    }
                }
            }
        }
        return 'success';
    }

    protected function spikeUser(){
        $user = AssembleUser::where('status',1)->select()->toArray();
        if ($user){
            foreach ($user as $key=>$value){
                if (time() > $value['end_time']){
                    $assemble = AssembleList::where('good_id',$value['good_id'])->find();
                    if ($assemble['analog'] == 1){
                        AssembleUser::update(['status'=>2],['id'=>$value['id']]);
                    }else{
                        AssembleUser::update(['status'=>0],['id'=>$value['id']]);
                    }
                }
            }

        }
        return 'success';
    }
}
